package com.kjlink.internal.workreport.project.controller;

import cn.xuqiudong.common.base.model.BaseResponse;
import cn.xuqiudong.common.base.model.PageInfo;
import cn.xuqiudong.common.base.model.Remind;
import cn.xuqiudong.common.base.vo.Select2VO;
import cn.xuqiudong.common.util.CommonUtils;
import com.kjlink.internal.workreport.base.controller.BaseController;
import com.kjlink.internal.workreport.base.enums.ProjectStatus;
import com.kjlink.internal.workreport.helper.SessionHelper;
import com.kjlink.internal.workreport.project.model.Project;
import com.kjlink.internal.workreport.project.service.ProjectService;
import com.kjlink.internal.workreport.project.vo.ProjectUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * 功能: :项目 控制层
 *
 * @author Vic.xu
 * @since 2024-04-29 13:39
 */
@RestController
@RequestMapping("/project")
public class ProjectController extends BaseController<ProjectService, Project> {

    /**
     * 可选的项目 列表
     */
    @RequestMapping(value = "optionalProject")
    public BaseResponse<List<Select2VO>> optionalJira(Integer companyId) {
        List<Project> projects = service.optionalProject(companyId);
        List<Select2VO> result = CommonUtils.listFilter(projects, p -> new Select2VO(p.getId(), p.getName()));
        return BaseResponse.success(result);
    }

    @Override
    protected String home() {
        return "/project/";
    }

    /**
     * 列表页
     */
    @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView index(Project lookup) {
        ModelAndView modelAndView = new ModelAndView("project/list");
        PageInfo<Project> pager = service.page(lookup);
        pager.setLookup(lookup);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }

    /**
     * 前往新增页面
     *
     * @return
     */
    @GetMapping("/form")
    public ModelAndView insert() {
        ModelAndView mv = new ModelAndView("project/form");
        return mv;
    }

    /**
     * 前往新增页面
     */
    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable Integer id, RedirectAttributes attrs) {

        Project entity = service.findById(id);
        if (entity == null) {
            return redirectHome("不存在的项目", attrs);
        }
        ModelAndView mv = new ModelAndView("project/form");
        mv.addObject("entity", entity);
        return mv;
    }


    @RequestMapping("/checkName")
    public BaseResponse<Boolean> checkCode(Integer id, String name) {
        boolean success = service.checkNotRepeat(id, name, "name");
        return BaseResponse.success(success);
    }


    @PostMapping("/save")
    public ModelAndView save(Project entity, RedirectAttributes attrs) {
        service.save(entity);
        return redirectHome(attrs);
    }

    /**
     * 项目用户详情页面
     *
     * @param id 项目id
     */
    @GetMapping("/projectUser/{id}")
    public ModelAndView projectUser(@PathVariable("id") Integer id, RedirectAttributes attrs) {
        ModelAndView mv = new ModelAndView("project/project-user");
        ProjectUser projectUser = service.projectUser(id);
        if (projectUser == null) {
            return redirectHome("不存在的项目", attrs);
        }
        mv.addObject("projectUser", projectUser);
        return mv;
    }

    /**
     * 新增项目人员
     */
    @PostMapping("/addProjectUser/{projectId}")
    public BaseResponse<?> addProjectUser(@PathVariable Integer projectId, @RequestParam("ids[]") Integer[] ids) {
        service.addProjectUser(projectId, ids);
        return BaseResponse.success();
    }

    @PostMapping("/deleteProjectUser/{userId}")
    public ModelAndView deleteProjectUser(@PathVariable Integer userId, Integer projectId, RedirectAttributes attrs) {
        ModelAndView mv = new ModelAndView("redirect:/project/projectUser/" + projectId);
        service.deleteProjectUser(userId, projectId);
        attrs.addFlashAttribute(Remind.success());
        return mv;
    }

    /**
     * 我的可选项目
     */
    @RequestMapping(value = "mine")
    public BaseResponse<List<Select2VO>> mine() {
        Integer userId = SessionHelper.currentUserId();
        List<Select2VO> list = service.mineProject(userId);
        return BaseResponse.success(list);
    }

    /**
     * 项目下拉列表
     *
     * @return
     */
    @RequestMapping(value = "all")
    public BaseResponse<List<Select2VO>> all() {
        List<Select2VO> list = service.mineProject(null);
        return BaseResponse.success(list);
    }


    /**
     * 删除
     */
    @GetMapping("/delete/{id}")
    public ModelAndView delete(@PathVariable Integer id, RedirectAttributes attrs) {
        service.delete(id);
        return redirectHome(attrs);
    }


    /**
     * 关闭
     */
    @GetMapping("/close/{id}")
    public ModelAndView close(@PathVariable Integer id, RedirectAttributes attrs) {
        updateStatus(id, ProjectStatus.CLOSED);
        return redirectHome(attrs);
    }

    /**
     * 打开
     */
    @GetMapping("/open/{id}")
    public ModelAndView open(@PathVariable Integer id, RedirectAttributes attrs) {
        updateStatus(id, ProjectStatus.OPEN);
        return redirectHome(attrs);
    }

    private void updateStatus(Integer id, ProjectStatus status) {
        Project project = new Project();
        project.setId(id);
        project.setStatus(status.getCode());
        service.update(project);
    }
}
